﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using MLRobotic.Tower.Common;
using System.Drawing;
using MLRobotic.Tower.Detection;
using System.Drawing.Imaging;

namespace MLRobotic.Tower.Common
{
    #region enums
    public enum Team
    {
        Red = 0,
        Blue = 1
    }

    public enum Phone
    {
        TouchPro2 = 0,
        Touch = 1,
    }

    public enum Lens
    {
        Standard = 0,
        Wide = 1,
        FishEye = 2
    }
    #endregion

    public class Config
    {
        private Team _team;
        private Lens _lens;
        private int _candleOffset = 177;

        public Config()
            : this(GetPhone())
        {
        }

        public Config(Phone phone)
        {
            InitHardwareSettings(phone);
            InitDefaultSettings(phone);
            InitColorsSettings();
        }

        #region Properties
        public Team Team { get { return _team; } set { _team = value; UpdateArea(); } }
        public Phone Phone { get; private set; }
        public Lens Lens { get { return _lens; } set { _lens = value; UpdateArea(); } }
        public int CandleOffset { get { return _candleOffset; } set { _candleOffset = value; UpdateArea(); } }
        public bool Master { get; set; }

        public int Width { get; set; }
        public int Height { get; set; }
        public PixelFormat PixelFormat { get; set; }

        public Rectangle CandleArea { get; private set; }
        public Rectangle BeaconArea { get; private set; }

        public TargetColor Red { get; set; }
        public TargetColor Blue { get; set; }
        public TargetColor Yellow { get; set; }
        public TargetColor Green { get; set; }
        public TargetColor Magenta { get; set; }
        #endregion

        public bool IsTouchPro2()
        {
            return Phone == Phone.TouchPro2;
        }

        private void UpdateArea()
        {
            switch (Lens)
            {
                case Lens.FishEye:
                    CandleArea = TransformArea(_candleOffset, 236, 60, 26);
                    BeaconArea = TransformArea(0, 194, 235, 34);
                    break;
                case Lens.Standard:
                    CandleArea = TransformArea(93, 249, 123, 50);
                    BeaconArea = TransformArea(0, 180, 240, 75);
                    break;
                case Lens.Wide:
                default:
                    throw new NotImplementedException();
            }
        }

        private Rectangle TransformArea(int x, int y, int w, int h)
        {
            // default to Touch2 and Red
            int ratio = Phone == Phone.TouchPro2 ? 1 : 2;

            if (Team == Team.Blue)
                x = Width - (x + w);

            return new Rectangle(x / ratio, y / ratio, w / ratio, h / ratio);
        }

        private void InitHardwareSettings(Phone phone)
        {
            switch (phone)
            {
                case Phone.TouchPro2:
                    Width = 240;
                    Height = 320;
                    break;
                case Phone.Touch:
                    Width = 120;
                    Height = 160;
                    break;
            }

            PixelFormat = PixelFormat.Format16bppRgb565;
            Phone = phone;
        }

        private void InitDefaultSettings(Phone phone)
        {
            Lens = Lens.FishEye;
            Team = Team.Red;
            Master = phone == Phone.TouchPro2;
        }

        private void InitColorsSettings()
        {
            // Coupe
            // Signal red : RAL 3001 = 163, 23, 26 = H 359
            // Traffic blue : RAL 5017 = 0, 59, 128 = H 212
            // Traffic Yellow : RAL 1023 = 252, 189, 31 = H 43

            // ML Robotic
            // Traffic red : RAL 3020 = 199, 23, 18 = H 1 90 78
            // Yellow : 
            // Green : 13, 81, 50 = H 153
            // Magenta : 167, 58, 97 = H 338

            //Red = new TargetColor(Color.Red, 0, 10, 
            //    0, 0.1, 1.0, 
            //    1, 0.1, 1.0, PixelFormat);
            //Blue = new TargetColor(Color.Blue, 300, 50, 
            //    0, 0.1, 1.0, 
            //    1, 0.1, 1.0, PixelFormat);
            Red = new TargetColor(Color.Red, 0, 10,
                0, 0.1, 1.0,
                1, 0.1, 1.0, PixelFormat);
            Blue = new TargetColor(Color.Blue, 250, 50,
                0, 0.1, 1.0,
                1, 0.1, 1.0, PixelFormat);
            Yellow = new TargetColor(Color.Yellow, 43, PixelFormat);
            Green = new TargetColor(Color.Green, 153, PixelFormat);
            Magenta = new TargetColor(Color.Magenta, 338, PixelFormat);
        }

        private static Phone GetPhone()
        {
            if (SystemInfo.IsTouchPro2())
                return Phone.TouchPro2;
            else
                return Phone.Touch;
        }
    }
}
